//
//  JiuDianHuiWuViewController.m
//  1243
//
//  Created by 袖手旁觀 on 16/4/19.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "JiuDianHuiWuViewController.h"
#import "JiuDianTableViewCell.h"
#import "NetWorkTools.h"
#import "ReadLoginData.h"

#define DeviceHeight            [[UIScreen mainScreen] bounds].size.height
#define DeviceWidth             [[UIScreen mainScreen] bounds].size.width

@interface JiuDianHuiWuViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *BuyPassWordTextField;
@property (weak, nonatomic) IBOutlet UITextField *peopleNumberTextField;

@property (weak, nonatomic) IBOutlet UITableView *HotelTableView;

@property (nonatomic,strong)NSArray *hotelArray;

@property (nonatomic,copy)NSString *HotelNumber;

@property(nonatomic,strong)NSMutableArray *boolArray;

@end

@implementation JiuDianHuiWuViewController

- (NSMutableArray *)boolArray
{
    if (!_boolArray) {
        _boolArray = [NSMutableArray arrayWithObjects:@"1",@"0", @"0", nil];
    }
    return _boolArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.HotelTableView.delegate = self;
    self.HotelTableView.dataSource = self;
    self.HotelTableView.rowHeight = 50;
    self.HotelTableView.bounces = NO;
    self.HotelTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    self.title = @"会晤";
    
//    self.view.backgroundColor = [UIColor orangeColor];
    
    [self loadHotelData];
 
}


- (IBAction)clickAccountButton:(id)sender {
    
    NSDictionary *dict = [ReadLoginData readLoginData];
    
    NSString *userNumber = [dict objectForKey:@"Number"];
    
    NSDictionary *params = @{
                             @"ToHotelNum":self.HotelNumber,
                             @"PersonNum":self.peopleNumberTextField.text,
                             @"FromUserNum":userNumber,
                             @"FromPassword":self.BuyPassWordTextField.text
                             };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"TURNACCOUNTHOTEL" completeBlock:^(id object) {
        
        NSString *message = [object objectForKey:@"Message"];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }];
    
}

- (void)loadHotelData {
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:@{} AndMenthodName:@"QURYHOTEL" completeBlock:^(id object) {
        
        NSString *hotelString = [object objectForKey:@"BackData"];
        
        self.hotelArray = [NetWorkTools arrayWithJsonString:hotelString];
        
        [self.HotelTableView reloadData];
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.hotelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JiuDianTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Jiucell"];
    
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"JiuDianTableViewCell" owner:nil options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.HotelLabel.text = [self.hotelArray[indexPath.row] objectForKey:@"RealName"];

    cell.selectView.layer.cornerRadius = CGRectGetHeight(cell.selectView.frame)/2;
    cell.selectView.clipsToBounds = YES;
    cell.selectView.layer.borderWidth = 1;
    
    
    if ([self.boolArray[indexPath.row] isEqualToString:@"1"]) {
        cell.selectView.backgroundColor = [UIColor greenColor];
    } else {
        cell.selectView.backgroundColor = [UIColor whiteColor];
    }
    
    return cell;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self clearTextField];
}

- (void)clearTextField {
    [self.BuyPassWordTextField resignFirstResponder];
    [self.peopleNumberTextField resignFirstResponder];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.HotelNumber = [self.hotelArray[indexPath.row] objectForKey:@"Number"];
    
    for (NSInteger i=0; i<self.boolArray.count; i++) {
        if (indexPath.row == i) {
            self.boolArray[i] = @"1";
        }else{
            self.boolArray[i] = @"0";
        }
    }
    
    [self.HotelTableView reloadData];
    
    NSLog(@"%ld",indexPath.row);
    NSLog(@"%@",self.HotelNumber);

}

@end
